<template>
	<view class="container">
		<view class="main">
			<view class="nav">
				<view class="header">
					<view class="left" v-if="orderType === 'takein'">
						<view class="store-name" @tap="tapStore">
							<text>{{ chooseStore.name }}</text>
							<view class="iconfont iconarrow-right"></view>
						</view>
						<view class="store-location">
							<image src='/static/images/order/location.png' style="width: 30rpx; height: 30rpx;"
								class="mr-10"></image>
							<text>距离您 {{ chooseStore.distance }}</text>
						</view>
					</view>
					<view class="left overflow-hidden" v-else>
						<view class="d-flex align-items-center overflow-hidden">
							<image src='/static/images/order/location.png' style="width: 30rpx; height: 30rpx;"
								class="mr-10"></image>
							<view class="font-size-extra-lg text-color-base font-weight-bold text-truncate">
								{{ choseAddress.street }}
							</view>
						</view>
						<view class="font-size-sm text-color-assist overflow-hidden text-truncate">
							由<text class="text-color-base"
								style="margin: 0 10rpx">{{ choseAddress.store_name }}</text>配送
						</view>
					</view>
					<view class="right">
						<view class="dinein" :class="{active: orderType === 'takein'}" @tap="tapTakein">
							<text>自取</text>
						</view>
						<view class="takeout" :class="{active: orderType === 'takeout'}" @tap="tapTakeOut">
							<text>外卖</text>
						</view>
					</view>
				</view>
				<view class="coupon">
					<text class="title">"霸气mini卡"超级购券活动，赶紧去购买</text>
					<view class="iconfont iconarrow-right"></view>
				</view>
			</view>
			<view class="content">
				<scroll-view class="menus" :scroll-into-view="menuScrollIntoView" scroll-with-animation scroll-y>
					<view class="wrapper">
						<view class="menu" :id="`menu-${item._id}`" :class="{'current': item._id === currentCateId}"
							v-for="(item, index) in goods" :key="index" @tap="handleMenuTap(item._id)">
							<text>{{ item.name }}</text>
							<view class="dot" v-show="menuCartNum(item._id)">{{ menuCartNum(item._id) }}</view>
						</view>
					</view>
				</scroll-view>
				<!-- goods list begin -->
				<scroll-view class="goods" scroll-with-animation scroll-y :scroll-top="cateScrollTop"
					@scroll="handleGoodsScroll">
					<view class="wrapper">
						<swiper class="ads" id="ads" autoplay :interval="3000" indicator-dots>
							<swiper-item v-for="(item, index) in ads" :key='index'>
								<image :src="item.image"></image>
							</swiper-item>
						</swiper>
						<view class="list">
							<!-- category begin -->
							<view class="category" v-for="(item, index) in goods" :key="index" :id="`cate-${item._id}`">
								<view class="title">
									<text>{{ item.name }}</text>
									<image :src="item.icon" class="icon"></image>
								</view>
								<view class="items">
									<!-- 商品 begin -->
									<view class="good" v-for="(good, key) in item.goods_list" :key="key">
										<image :src="good.images" class="image" @tap="showGoodDetailModal(item, good)">
										</image>
										<view class="right">
											<text class="name">{{ good.name }}</text>
											<text class="tips">{{ good.content }}</text>
											<view class="price_and_action">
												<text class="price">￥{{ good.price }}</text>
												<view class="btn-group" v-if="good.use_property">
													<button type="primary" class="btn property_btn" hover-class="none"
														size="mini" @tap="showGoodDetailModal(item, good)">
														选规格
													</button>
													<view class="dot" v-show="goodCartNum(good._id)">
														{{ goodCartNum(good._id) }}
													</view>
												</view>
												<view class="btn-group" v-else>
													<button type="default" v-show="goodCartNum(good._id)" plain
														class="btn reduce_btn" size="mini" hover-class="none"
														@tap="handleReduceFromCart(item, good)">
														<view class="iconfont iconsami-select"></view>
													</button>
													<view class="number" v-show="goodCartNum(good._id)">
														{{ goodCartNum(good._id) }}
													</view>
													<button type="primary" class="btn add_btn" size="min"
														hover-class="none" @tap="handleAddToCart(item, good, 1)">
														<view class="iconfont iconadd-select"></view>
													</button>
												</view>
											</view>
										</view>
									</view>
									<!-- 商品 end -->
								</view>
							</view>
							<!-- category end -->
						</view>
					</view>
				</scroll-view>
				<!-- goods list end -->
			</view>
			<!-- content end -->
			<!-- 购物车栏 begin -->
			<view class="cart-box" v-if="cart.length > 0">
				<view class="mark">
					<image src="/static/images/menu/cart.png" class="cart-img" @tap="openCartPopup"></image>
					<view class="tag">{{ getCartGoodsNumber }}</view>
				</view>
				<view class="price">￥{{ getCartGoodsPrice }}</view>
				<button type="primary" class="pay-btn" @tap="toPay" :disabled="disabledPay">
					{{ disabledPay ? `差${spread}元起送` : '去结算' }}
				</button>
			</view>
			<!-- 购物车栏 end -->
		</view>
		<!-- 商品详情模态框 begin -->
		<modal :show="goodDetailModalVisible" class="good-detail-modal" color="#5A5B5C" width="90%" custom
			padding="0rpx" radius="12rpx">
			<view class="cover">
				<image v-if="good.images" :src="good.images" class="image"></image>
				<view class="btn-group">
					<image src="/static/images/menu/share-good.png"></image>
					<image src="/static/images/menu/close.png" @tap="closeGoodDetailModal"></image>
				</view>
			</view>
			<scroll-view class="detail" scroll-y>
				<view class="wrapper">
					<view class="basic">
						<view class="name">{{ good.name }}</view>
						<view class="tips">{{ good.content }}</view>
					</view>
					<view class="properties" v-if="good.use_property">
						<view class="property" v-for="(item, index) in good.property" :key="index">
							<view class="title">
								<text class="name">{{ item.name }}</text>
								<view class="desc" v-if="item.desc">({{ item.desc }})</view>
							</view>
							<view class="values">
								<view class="value" v-for="(value, key) in item.values" :key="key"
									:class="{'default': value.is_default}" @tap="changePropertyDefault(index, key)">
									{{ value.value }}
								</view>
							</view>
						</view>
					</view>
				</view>
			</scroll-view>
			<view class="action">
				<view class="left">
					<view class="price">￥{{ good.price }}</view>
					<view class="props" v-if="getGoodSelectedProps(good)">
						{{ getGoodSelectedProps(good) }}
					</view>
				</view>
				<view class="btn-group">
					<button type="default" plain class="btn" size="mini" hover-class="none" @tap="handlePropertyReduce">
						<view class="iconfont iconsami-select"></view>
					</button>
					<view class="number">{{ good.number }}</view>
					<button type="primary" class="btn" size="min" hover-class="none" @tap="handlePropertyAdd">
						<view class="iconfont iconadd-select"></view>
					</button>
				</view>
			</view>
			<view class="add-to-cart-btn" @tap="handleAddToCartInModal">
				<view>加入购物车</view>
			</view>
		</modal>
		<!-- 商品详情模态框 end -->
		<!-- 购物车详情popup -->
		<popup-layer direction="top" :show-pop="cartPopupVisible" class="cart-popup">
			<template slot="content">
				<view class="top">
					<text @tap="handleCartClear">清空</text>
				</view>
				<scroll-view class="cart-list" scroll-y>
					<view class="wrapper">
						<view class="item" v-for="(item, index) in cart" :key="index">
							<view class="left">
								<view class="name">{{ item.name }}</view>
								<view class="props">{{ item.props_text }}</view>
							</view>
							<view class="center">
								<text>￥{{ item.price }}</text>
							</view>
							<view class="right">
								<button type="default" plain size="mini" class="btn" hover-class="none"
									@tap="handleCartItemReduce(index)">
									<view class="iconfont iconsami-select"></view>
								</button>
								<view class="number">{{ item.number }}</view>
								<button type="primary" class="btn" size="min" hover-class="none"
									@tap="handleCartItemAdd(index)">
									<view class="iconfont iconadd-select"></view>
								</button>
							</view>
						</view>
						<view class="item" v-if="orderType == 'takeout' && chooseStore.packing_fee">
							<view class="left">
								<view class="name">包装费</view>
							</view>
							<view class="center">
								<text>￥{{ parseFloat(chooseStore.packing_fee) }}</text>
							</view>
							<view class="right invisible">
								<button type="default" plain size="mini" class="btn" hover-class="none">
									<view class="iconfont iconsami-select"></view>
								</button>
								<view class="number">1</view>
								<button type="primary" class="btn" size="min" hover-class="none">
									<view class="iconfont iconadd-select"></view>
								</button>
							</view>
						</view>
					</view>
				</scroll-view>
			</template>
		</popup-layer>
		<!-- 购物车详情popup -->
	</view>
	<!-- 	<view class="loading" v-else>
		<image src="/static/images/loading.gif"></image>
	</view> -->
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex'
	import modal from '../../components/modal/modal.vue'
	import popupLayer from '../../components/popup-layer/popup-layer.vue'
	export default {
		data() {
			return {
				goods: [],
				ads: [{
						image: 'https://img-shop.qmimg.cn/s23107/2020/04/27/4ebdb582a5185358c4.jpg?imageView2/2/w/600/h/600'
					},
					{
						image: 'https://images.qmai.cn/s23107/2020/05/08/c25de6ef72d2890630.png?imageView2/2/w/600/h/600'
					},
					{
						image: 'https://img-shop.qmimg.cn/s23107/2020/04/10/add546c1b1561f880d.jpg?imageView2/2/w/600/h/600'
					},
					{
						image: 'https://images.qmai.cn/s23107/2020/04/30/b3af19e0de8ed42f61.jpg?imageView2/2/w/600/h/600'
					},
					{
						image: 'https://img-shop.qmimg.cn/s23107/2020/04/17/8aeb78516d63864420.jpg?imageView2/2/w/600/h/600'
					}
				],
				menuScrollIntoView: '',
				currentCateId: 6905, //默认分类
				cateScrollTop: 0,
				cart: [],
				good: {}, //当前饮品
				goodDetailModalVisible: false,
				cartPopupVisible: false
			}
		},
		components: {
			modal,
			popupLayer
		},
		computed: {
			...mapState(['chooseStore', 'orderType', 'choseAddress', 'isLogin']),
			goodCartNum() {
				return (id) => this.cart.reduce((acc, cur) => {
					if (cur.id === id) {
						return acc += cur.number
					}
					return acc
				}, 0)
			},
			menuCartNum() {
				return (id) => this.cart.reduce((acc, cur) => {
					if (cur.cate_id === id) {
						return acc += cur.number
					}
					return acc
				}, 0)
			},
			getCartGoodsNumber() { //计算购物车总数
				return this.cart.reduce((acc, cur) => acc + cur.number, 0)
			},
			getCartGoodsPrice() { //计算购物车总价
				console.log('cart', this.cart)
				return this.cart.reduce((acc, cur) => acc + cur.number * cur.price, 0)
			},
			disabledPay() {
				return this.orderType === 'takeout' && (this.getCartGoodsPrice < 38) ? true : false
			},
			spread() {
				if (this.orderType !== 'takeout') return
				return parseFloat((38 - this.getCartGoodsPrice).toFixed(2))
			}
		},
		onLoad() {
			this.init()
		},
		methods: {
			...mapMutations(['SET_ORDER_TYPE']),
			tapTakein() {
				if (Object.keys(this.chooseStore).length !== 0) {
					this.SET_ORDER_TYPE('takein')
				} else {
					uni.navigateTo({
						url: '../stores/stores'
					})
				}
			},
			tapTakeOut() {
				if (!this.isLogin) {
					uni.navigateTo({
						url: '../login/login'
					})
					return
				}
				uni.navigateTo({
					url: '../address/address'
				})
			},
			openCartPopup() { //打开/关闭购物车列表popup
				this.cartPopupVisible = !this.cartPopupVisible
			},
			handleCartClear() { //清空购物车
				uni.showModal({
					title: '提示',
					content: '确定清空购物车么',
					success: ({
						confirm
					}) => {
						if (confirm) {
							this.cartPopupVisible = false
							this.cart = []
						}
					}
				})
			},
			handleCartItemAdd(index) {
				this.cart[index].number += 1
			},
			handleCartItemReduce(index) {
				if (this.cart[index].number === 1) {
					this.cart.splice(index, 1)
				} else {
					this.cart[index].number -= 1
				}
				if (!this.cart.length) {
					this.cartPopupVisible = false
				}
			},
			// 计算右侧goods的top和bottom的高度
			calcSize() {
				let h = 10

				let view = uni.createSelectorQuery().select('#ads')
				view.fields({
					size: true
				}, data => {
					h += Math.floor(data.height)
				}).exec()

				this.goods.forEach(item => {
					console.log('item', item)
					// 计算右侧每个good的高度
					let view = uni.createSelectorQuery().select(`#cate-${item._id}`)
					view.fields({
						size: true
					}, data => {
						item.top = h
						h += data.height
						item.bottom = h
					}).exec()
				})
				this.sizeCalcState = true
			},
			handleMenuTap(id) { //点击菜单项事件
				if (!this.sizeCalcState) {
					this.calcSize()
				}

				this.currentCateId = id
				this.$nextTick(() => this.cateScrollTop = this.goods.find(item => item._id === id).top)
			},
			handleGoodsScroll({
				detail
			}) { //商品列表滚动事件
				console.log('detail', detail)
				if (!this.sizeCalcState) {
					this.calcSize()
				}
				const {
					scrollTop
				} = detail
				let tabs = this.goods.filter(item => item.top <= scrollTop).reverse()
				if (tabs.length > 0) {
					this.currentCateId = tabs[0]._id
				}
			},
			handleReduceFromCart(item, good) {
				console.log('cart', this.cart)
				const index = this.cart.findIndex(item => item.id === good._id)
				console.log('index', index)
				this.cart[index].number -= 1
				if (this.cart[index].number <= 0) {
					this.cart.splice(index, 1)
				}
			},
			handleAddToCart(cate, good, num) {
				const index = this.cart.findIndex(item => item.id === good._id)
				if (index > -1) {
					this.cart[index].number += num
				} else {
					this.cart.push({
						id: good._id,
						cate_id: cate._id,
						name: good.name,
						price: good.price,
						number: num,
						image: good.images,
						props_text: good.props_text,
						property: good.property,
						use_property: good.use_property
					})
				}
			},
			tapStore() {
				uni.navigateTo({
					url: '../stores/stores'
				})
			},
			init() {
				if (Object.keys(this.chooseStore).length === 0 && this.orderType === 'takein') {
					uni.navigateTo({
						url: '../stores/stores'
					})
				}
				return uniCloud.callFunction({
					name: 'goods'
				}).then(res => {
					console.log('res', res)
					this.goods = res.result.data
				})
			},
			closeGoodDetailModal() {
				this.goodDetailModalVisible = false
				this.good = {}
				this.category = {}
			},
			changePropertyDefault(index, key) {
				this.good.property[index].values.forEach(value => this.$set(value, 'is_default', 0))
				this.good.property[index].values[key].is_default = 1
				this.good.number = 1
			},
			getGoodSelectedProps(good, type = 'text') {
				console.log('goods12', good)
				if (good.property) {
					let props = []
					good.property.forEach(({
						values
					}) => {
						values.forEach(value => {
							if (value.is_default) {
								props.push(type === 'text' ? value.value : value.id)
							}
						})
					})
					return type === 'text' ? props.join(',') : props
				}
				return ''
			},
			handlePropertyAdd() {
				this.good.number += 1
			},
			handlePropertyReduce() {
				if (this.good.number === 1) return
				this.good.number -= 1
			},
			handleAddToCartInModal() {
				console.log('this.good1')
				const product = Object.assign({}, this.good, {
					props_text: this.getGoodSelectedProps(this.good),
					props: this.getGoodSelectedProps(this.good, 'id')
				})
				this.handleAddToCart(this.category, product, this.good.number)
				this.closeGoodDetailModal()
			},
			showGoodDetailModal(item, good) {
				console.log('this.good2', good)
				this.good = JSON.parse(JSON.stringify({
					...good,
					number: 1
				}))
				this.category = JSON.parse(JSON.stringify(item))
				this.goodDetailModalVisible = true
				console.log('goodDetailModalVisible', this.goodDetailModalVisible)
			},
			toPay() {
				if (!this.isLogin) {
					uni.navigateTo({
						url: '/pages/login/login'
					})
					return
				}

				uni.showLoading({
					title: '加载中'
				})
				// uni.setStorageSync('cart', JSON.parse(JSON.stringify(this.cart)))
				
				return uniCloud.callFunction({
					name: 'validdateToken',
					data: {
						token: uni.getStorageSync('token')
					}
				}).then(res => {
					if(res.result.status === 0) {
						uni.hideLoading()
						if(this.orderType === 'takein') {
							let data = {
								openId: res.result.openId,
								goodsInOrder: this.cart,
								chooseStore: this.chooseStore.name
							}
							
							return uniCloud.callFunction({
								name: 'order',
								data: {
									data: data,
									action: 'addTakein'
								}
							}).then(resData => {
								console.log('resData', resData)
								uni.navigateTo({
									url: '/pages/pay/pay?order_id=' + resData.result.order_id
								})
							})
						}else if(this.orderType === 'takeout') {
							let data = {
							openId: res.result.openId,
							goodsInOrder: this.cart,	
							chooseStore: this.choseAddress.storeName,
							order_address: this.choseAddress._id
							}
							
							return uniCloud.callFunction({
								name: 'order',
								data: {
									data: data,
									action: 'addTakeout'
								}
							}).then(resData => {
								console.log('resData', resData.result.order_id)
								uni.navigateTo({
									url: '/pages/pay/pay?order_id=' + resData.result.order_id
								})
							})
						} else {
							uni.hideLoading()
							uni.showModal({
								content: res.result.msg,
								showCancel: false
							})
						}
					}
				})
				
			}
		}
	}
</script>

<style lang="scss" scoped>
	@import '~@/pages/menu/menu.scss';
</style>
